package xyz.eden.cloud.common.web.did;

import lombok.AllArgsConstructor;
import xyz.eden.cloud.common.web.did.enums.IdType;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@AllArgsConstructor
public class IdGenFunction {

    private List<IdGenComponent> componentList;

    public String getStrId(IdType idType) {
        IdGenComponent idGenComponent = getIdGenComponent(idType);
        return Objects.isNull(idGenComponent) ? null : idGenComponent.getStrId();
    }

    public Long getId(IdType idType) {
        IdGenComponent idGenComponent = getIdGenComponent(idType);
        return Objects.isNull(idGenComponent) ? null : idGenComponent.getId();
    }

    private IdGenComponent getIdGenComponent(IdType idType) {
        IdType type = Objects.isNull(idType) ? IdType.DEFAULT : idType;
        return Optional.ofNullable(componentList).orElse(Collections.emptyList())
                .stream().filter(component -> {
                    return component.isSupport(type);
                }).findFirst().orElse(null);
    }
}
